package com.fastadmin.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fastadmin.mapper.impl.GongGaoMapper;
import com.fastadmin.pojo.FastParam;
import com.fastadmin.pojo.GongGao;
import com.xphsc.easyjdbc.page.PageInfo;
import lombok.RequiredArgsConstructor;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequiredArgsConstructor
public class GongGaoController extends BaseController {

    private final GongGaoMapper gongGaoMapper;


    @GetMapping("sys/gonggao/{path}")
    public String index(@PathVariable String path) {
        return "sys/gonggao/" + path;
    }

    @GetMapping("sys/gonggao/edit")
    public String index(@RequestParam String ids, Model model) {
        GongGao gonggao = this.gongGaoMapper.findById(ids);
        model.addAttribute("gonggao", gonggao);
        return "sys/gonggao/edit";
    }


    @RequestMapping("sys/gonggao/data")
    @ResponseBody
    public Object data(FastParam fastParam) {
        PageInfo<GongGao> pageInfo = this.gongGaoMapper.findByFastParam(fastParam);
        for (GongGao gongGao : pageInfo.list) {
            gongGao.setContent(Jsoup.parse(gongGao.getContent()).text());
        }
        return datagrid(pageInfo);
    }

    @RequestMapping("sys/gonggao/del")
    @ResponseBody
    public Object del(String ids) {
        String[] split = StrUtil.split(ids, ",");
        for (String s : split) {
            gongGaoMapper.deleteById(s);
        }
        return ok();
    }

    @RequestMapping("sys/gonggao/save")
    @ResponseBody
    public Object save(GongGao gongGao) {
        if (ObjectUtil.isNotNull(gongGao.getId())) {
            //修改
            gongGaoMapper.update(gongGao);
        } else {
            //保存
            gongGaoMapper.save(gongGao);
        }
        return ok();
    }

}
